package com.ruoyi.web.controller.classs;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.TextToClasses.domain.ClassText;
import com.ruoyi.TextToClasses.service.IClassTextService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.UserClass;
import com.ruoyi.system.domain.UserTextClass;
import com.ruoyi.system.service.IUserClassService;
import com.ruoyi.texts.domain.Text;
import com.ruoyi.texts.domain.TextDetails;
import com.ruoyi.texts.service.ITextService;
import com.ruoyi.useres.domain.User;
import com.ruoyi.useres.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.classs.domain.Classes;
import com.ruoyi.classs.service.IClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级管理Controller
 *
 * @author 石晨臣
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/classs/classes")
public class ClassesController extends BaseController
{
    @Autowired
    private IUserClassService userClassService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITextService textService;
    @Autowired
    private IClassTextService classTextService;
    @Autowired
    private IClassesService classesService;

    /**
     * 查询班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('classs:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes)
    {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 导出班级管理列表
     */
    @PreAuthorize("@ss.hasPermi('classs:classes:export')")
    @Log(title = "班级管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes)
    {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "班级管理数据");
    }

    /**
     * 获取班级管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('classs:classes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(classesService.selectClassesById(id));
    }

    /**
     * 新增班级管理
     */
//    @PreAuthorize("@ss.hasPermi('classs:classes:add')")
//    @Log(title = "班级管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes)
    {
        classes.setCreateUser(SecurityUtils.getUserId().toString());
        classes.setUpdateBy(SecurityUtils.getUserId().toString());
        classes.setUpdateTime(DateUtils.getNowDate());
        classes.setCreateTime(DateUtils.getNowDate());
        classes.setTeacherId(SecurityUtils.getUserId());
        classes.setNowNum(0L);
        classes.setStatus(1L);
        classesService.insertClasses(classes);
        List<Classes> classes1 = classesService.selectClassesList(null);
        Long classId = classes1.get(classes1.size()-1).getId();
        UserClass userClass = new UserClass();
        userClass.setClassId(classId);
        userClass.setUserId(SecurityUtils.getUserId());
        userClassService.insertUserClass(userClass);
        return toAjax(true);
    }

    /**
     * 修改班级管理
     */
    @PreAuthorize("@ss.hasPermi('classs:classes:edit')")
    @Log(title = "班级管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes)
    {
        classes.setUpdateBy(SecurityUtils.getUserId().toString());
//        classes.s
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除班级管理
     */
    @PreAuthorize("@ss.hasPermi('classs:classes:remove')")
    @Log(title = "班级管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(classesService.deleteClassesByIds(ids));
    }

    @DeleteMapping("/class/{id}")
    public AjaxResult remove(@PathVariable Long id){
        return toAjax(classesService.deleteClassesById(id));
    }
    //查询某个老师创建的班级
    @GetMapping("/classByTeacher/{id}")
    public AjaxResult selectAllByTeacherId(@PathVariable Long id){
        return success(classesService.selectAllByTeacherId(id));
    }

//查询某个人下所有班级
    @GetMapping("/user")
    public AjaxResult selectAllByUserId(){
        return success(classesService.selectAllByUserId(SecurityUtils.getUserId()));
    }
//    查看所有班级以及班主任
    @GetMapping("/details/list")
    public AjaxResult selectAllAndDetailList(){
        return success(classesService.selectAllAndDetailList());
    }

//    在班级里加学生
    @GetMapping("/addUser/{classId}")
    public AjaxResult selectAllAndUserList(@PathVariable Long classId){
        UserClass userClass1 = new UserClass();
        userClass1.setClassId(classId);
        List<UserClass> userClasses = userClassService.selectUserClassList(userClass1);
        for (UserClass userClass : userClasses) {
            if(userClass.getUserId().equals(SecurityUtils.getUserId())){
                return error("加班级重复");
            }
        }
        UserClass userClass = new UserClass();
        userClass.setClassId(classId);
        userClass.setUserId(SecurityUtils.getUserId());
        return success(userClassService.insertUserClass(userClass));
    }

//    在班级里加试卷
    @PostMapping("/addText")
    public AjaxResult selectAllAndTextList(@RequestBody Map map){
        Object name = map.get("name");
        Object introduction = map.get("introduction");
        Object classId = map.get("classId");
        Text text = new Text();
        text.setCreateTime(DateUtils.getNowDate());
        text.setName(name.toString());
        text.setTeacherId(SecurityUtils.getUserId());
        text.setUpdateTime(DateUtils.getNowDate());
        text.setCreateUser(SecurityUtils.getUserId().toString());
        text.setUpdateUser(SecurityUtils.getUserId().toString());
        text.setStatus("1");
        text.setClassId(Long.valueOf(classId.toString()));
        int i = textService.insertText(text);
        Long textId = textService.selectTextList(null).get(textService.selectTextList(null).size()-1).getId();
        ClassText classText = new ClassText();
        classText.setTextId(textId);
        classText.setClassId(Long.valueOf(classId.toString()));
        classTextService.insertClassText(classText);
        System.err.println(map);
//        ClassText classText = new ClassText();
//        classText.setClassId(classId);
//        classText.setTextId(textId);
//        classTextService.insertClassText(classText)
        return success(true);
    }
    @GetMapping("/classByPowerId/{name}")
    public AjaxResult selectAllByPowerId(@PathVariable String name){
        return success(classesService.selectAllByPowerId(name));
    }
}
